﻿package com.dykj.apps.web.sysmanager.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dykj.apps.web.sysmanager.dao.IFunctionDAO;
import com.dykj.apps.web.sysmanager.dao.po.SysFunction;
import com.dykj.apps.web.sysmanager.dto.FunctionDTO;
import com.dykj.platform.framework.exception.ApplicationException;
import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.utils.BeanUtils;

public class FunctionBSImpl implements IFunctionBS {
	private IFunctionDAO dao;
	/**
	 *  取得树形功能菜单
	 */
	public List getFuntion(String parentid,String roleid,String type) throws ApplicationException {
		List<FunctionDTO> function = new ArrayList<FunctionDTO>();
		try {
			List list =null;
			if("0".equals(type)||"1".equals(type)){		//如果是超级管理员
				list = dao.findFunctionForAdmin(parentid, type);		//查询全部管理员菜单
			}else{
				list = dao.findFunctionByRole(parentid, roleid);//查询用户菜单
			}
			for(int i=0;i<list.size();i++){
					SysFunction fun= (SysFunction)list.get(i);
					FunctionDTO dto = new FunctionDTO();
					BeanUtils.copyProperties(dto, fun);
					if("0".equals(type)||"1".equals(type)){
						if(dao.getFunctionCout(fun.getFunctionid(),"0")>0){ //如果有下级菜单
							dto.setIsfolder("true");
						}				
					}else{
						List subList = dao.findFunctionByRole(dto.getFunctionid(), roleid);//查询用户菜单
						for(int j=0;j<subList.size();j++){
							SysFunction subFun= (SysFunction)subList.get(j);
							FunctionDTO subdto = new FunctionDTO();
							BeanUtils.copyProperties(subdto, subFun);
							if("1".equals(subdto.getType())){
								function.add(subdto);
							}else if("0".equals(subdto.getType())){
								dto.setIsfolder("true");
							}
						}
					}
					function.add(dto);
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}

		return function;
	}
	public List getManagerFunction(String parentid,String roleid,String type) throws ApplicationException {
		List<FunctionDTO> function = new ArrayList<FunctionDTO>();
		try {
			List list = dao.findFunctionById(parentid);//查询管理员菜单
			for(int i=0;i<list.size();i++){
					SysFunction fun= (SysFunction)list.get(i);
					FunctionDTO dto = new FunctionDTO();
					BeanUtils.copyProperties(dto, fun);
					if(dao.getFunctionCout(fun.getFunctionid(),null)>0){//如果有下级菜单
						dto.setIsfolder("true");
					}
					dto.setHref("findFunctionAction!modifyFunction.action?functionid="+fun.getFunctionid());
					function.add(dto);
			}
			
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}

		return function;
	}
	public FunctionDTO getFunctionDTO(String id) throws ApplicationException {
		FunctionDTO dto = new FunctionDTO();
		try {
			if(id!=null){
				SysFunction fun = (SysFunction)dao.getObject(SysFunction.class, id);
				if(fun!=null){
					BeanUtils.copyProperties(dto, fun);
				}
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return dto;
	}
	public int delFunction(String id) throws ApplicationException {
		try {
			if(dao.getFunctionCout(id,null)>0){
				return -1;
			}else{
				dao.delByKey(SysFunction.class, id);
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return 0;
	}
	public int saveOrUpdateFunction(FunctionDTO dto) throws ApplicationException {
		SysFunction fun = new SysFunction();
		try {
			BeanUtils.copyProperties(fun, dto);
			if(fun.getFunctionid()==null || "".equals(fun.getFunctionid())){
				fun.setFunctionid(dao.getKeyIndex("functionid", "sysfunction"));
				dto.setFunctionid(fun.getFunctionid());
			}
			fun.setDatecreated(new Date());
			dao.saveOrUpdate(fun);
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return 0;
	}
	
	/*
	 * ================================get set方法=====================================
	 */	
	public IFunctionDAO getDao() {
		return dao;
	}
	public void setDao(IFunctionDAO dao) {
		this.dao = dao;
	}


}
